package main

import (
	"context"
	"fmt"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/user_srv/proto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

var (
	userClient proto.UserClient
	conn       *grpc.ClientConn
)

func Init() {
	var err error
	conn, err = grpc.NewClient("127.0.0.1:4241", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic("create client connect failed: " + err.Error())
	}
	userClient = proto.NewUserClient(conn)
}

func TestGetUserList() {
	// 客户端
	resp, err := userClient.GetUserList(context.Background(), &proto.PageInfo{Pn: 1, PSize: 5})
	if err != nil {
		panic(err)
	}
	for _, user := range resp.Data {
		fmt.Println(user.UserId, user.Phone, user.NickName, user.Password, user.Birthday)
		res, err := userClient.CheckPassword(context.Background(), &proto.CheckPwdInfo{
			Password:          "admin123",
			EncryptedPassword: user.Password,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println(res.Success)
	}
}

func TestCreateUser() {
	for i := 0; i < 10; i++ {
		resp, err := userClient.CreateUser(context.Background(), &proto.CreateUserInfo{
			NickName: fmt.Sprintf("Tom_test%d", i),
			Password: "admin123",
			Phone:    fmt.Sprintf("1553256845%d", i),
		})
		if err != nil {
			panic(err)
		}
		fmt.Println("id: ", resp.Id)
		fmt.Println("user_id: ", resp.UserId)
	}
}

func TestGetUserByUserId() {
	resp, err := userClient.GetUserByUserId(context.Background(), &proto.UserIdReq{
		UserId: 1888799064745578496,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(resp.Id, resp.UserId, resp.Phone, resp.NickName, resp.Password, resp.Birthday)
}

func main() {
	Init()
	//TestCreateUser()
	TestGetUserList()
	//TestGetUserByUserId()
	conn.Close()
}
